import http from './http';
import { requestWithRetry } from '@/utils/retry';

// 发货管理API服务
export const deliveryAPI = {
  // 获取发货订单列表
  async getDeliveryOrders(params: any) {
    try {
      // 使用重试机制获取发货订单列表
      const response: any = await requestWithRetry(
        () => http.get('/delivery/orders', { params }),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: {
          list: response.data?.list || [],
          pagination: response.data?.pagination || {
            page: 1,
            limit: 20,
            total: 0
          }
        }
      };
    } catch (error: any) {
      return {
        success: false,
        data: {
          list: [],
          pagination: null
        },
        message: error.message || '获取发货订单列表失败'
      };
    }
  },

  // 获取发货记录列表
  async getDeliveryRecords(orderId?: string) {
    try {
      // 使用重试机制获取发货记录列表
      const response: any = await requestWithRetry(
        () => http.get('/delivery-records', { 
          params: { orderId }
        }),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: {
          list: response.data?.list || [],
          pagination: response.data?.pagination || {
            page: 1,
            limit: 20,
            total: 0
          }
        }
      };
    } catch (error: any) {
      return {
        success: false,
        data: {
          list: [],
          pagination: null
        },
        message: error.message || '获取发货记录列表失败'
      };
    }
  },

  // 获取发货记录详情
  async getDeliveryRecord(id: string) {
    try {
      // 使用重试机制获取发货记录详情
      const response = await requestWithRetry(
        () => http.get(`/delivery-records/${id}`),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      const data = response.data || response;
      return {
        success: true,
        data: data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '获取发货记录详情失败'
      };
    }
  },

  // 创建发货记录
  async createDeliveryRecord(recordData: any) {
    try {
      const response = await http.post('/delivery-records', recordData);
      const data = response.data || response;
      return {
        success: true,
        data: data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '创建发货记录失败'
      };
    }
  },

  // 更新发货记录
  async updateDeliveryRecord(id: string, recordData: any) {
    try {
      const data = await http.put(`/delivery-records/${id}`, recordData);
      return {
        success: true,
        data: data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '更新发货记录失败'
      };
    }
  },

  // 删除发货记录
  async deleteDeliveryRecord(id: string) {
    try {
      const data = await http.delete(`/delivery-records/${id}`);
      return {
        success: true,
        data: data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '删除发货记录失败'
      };
    }
  },

  // 获取发货统计信息
  async getDeliveryStats() {
    try {
      // 使用重试机制获取发货统计信息
      const response = await requestWithRetry(
        () => http.get('/delivery/stats'),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: {
          total: 0,
          pending: 0,
          shipped: 0,
          delivered: 0
        },
        message: error.message || '获取发货统计信息失败'
      };
    }
  },

  // 获取智能发货建议
  async getSmartDeliverySuggestions(orderId: string) {
    try {
      // 使用重试机制获取智能发货建议
      const response = await requestWithRetry(
        () => http.get('/reports/delivery-suggestions', { 
          params: { orderId }
        }),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: {
          suggestions: [],
          priority: 'normal',
          recommendedAction: ''
        },
        message: error.message || '获取智能发货建议失败'
      };
    }
  },
  
  // 更新销售订单的发货状态
  async updateDeliveryStatus(orderId: string, statusData: any) {
    try {
      const data = await http.put(`/sales-orders/${orderId}/delivery-status`, statusData);
      return {
        success: true,
        data: data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '更新发货状态失败'
      };
    }
  }
};